<template>
  <div class="centreLeft1">
    <div class="bg-color-black">
      <div class="d-flex pt-2 pl-2">
        <span>
          <i class="iconfont icon-tongji4" /> 
        </span>
        <div class="d-flex">
          <span class="fs-xl text mx-2">发电量统计(kWh)</span>
        </div>
      </div>
      <div class="d-flex jc-center">
<!--        <div id="sector" style="width: 100%; height:240px;">-->
<!--        </div>-->
        <Chart />
      </div>
      <!-- 4个主要的数据 -->
      <div class="bottom-data">
        <div class="item-box mt-2" v-for="(item, index) in numberData" :key="index">
          <div class="d-flex jc-end">
            <dv-digital-flop class="dv-digital-flop" :config="item.config" />
          </div>
          <p>
            <span> {{ item.text }} </span>
            <span class="colorYellow">(kWh)</span>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import {computed, defineComponent, onMounted, onUnmounted, reactive, watch} from 'vue'
import Chart from './chart/index'
import * as echarts from 'echarts';
import {useWebSocketStore} from "@/store/websocket";
import {useStore} from "vuex";
//axios

export default defineComponent({
  components: {
    Chart
  },
  setup() {
    // 下层数据
    const wsStore = useWebSocketStore()
    const store = useStore()
    const strategy = computed(() => store.getters.currentStrategy) // 策略列表
    const totalResult = computed(() => ({
      totalPower : wsStore.cdata[strategy.value].totalSolarPower + wsStore.cdata[strategy.value].totalBatteryPower
                  +wsStore.cdata[strategy.value].totalGridPower + wsStore.cdata[strategy.value].totalWindPower,
      averagePower : ((wsStore.cdata[strategy.value].totalSolarPower + wsStore.cdata[strategy.value].totalBatteryPower
          +wsStore.cdata[strategy.value].totalGridPower + wsStore.cdata[strategy.value].totalWindPower) / 4.0).toFixed(2),
      maxPower : Math.max(wsStore.cdata[strategy.value].totalSolarPower, wsStore.cdata[strategy.value].totalBatteryPower,
                      wsStore.cdata[strategy.value].totalGridPower, wsStore.cdata[strategy.value].totalWindPower),
      minPower : Math.min(wsStore.cdata[strategy.value].totalSolarPower, wsStore.cdata[strategy.value].totalBatteryPower,
          wsStore.cdata[strategy.value].totalGridPower, wsStore.cdata[strategy.value].totalWindPower)
    }))

    // const dataArr = [
    //   {
    //     number: 150,
    //     text: '总发电量'
    //   },
    //   {
    //     number: 144,
    //     text: '平均发电量'
    //   },
    //   {
    //     number: 361,
    //     text: '最高发电量'
    //   },
    //   {
    //     number: 571,
    //     text: '最低发电量'
    //   }
    // ]
    // 对应图标
    // const iconFont = [
    //   'icon-diagnose',
    //   'icon-monitoring',
    //   'icon-cloudupload',
    //   'icon-clouddownload'
    // ]
    // 数字显示数据（改为响应式）
    const numberData = reactive([
      {
        config: {
          number: [0], // 初始值设为0，后面会更新
          toFixed: 1,
          content: '{nt}',
          style: { fontSize: 24 }
        },
        text: '总发电量'
      },
      {
        config: {
          number: [0],
          toFixed: 2,
          content: '{nt}',
          style: { fontSize: 24 }
        },
        text: '平均发电量'
      },
      {
        config: {
          number: [0],
          toFixed: 1,
          content: '{nt}',
          style: { fontSize: 24 }
        },
        text: '最高发电量'
      },
      {
        config: {
          number: [0],
          toFixed: 1,
          content: '{nt}',
          style: { fontSize: 24 }
        },
        text: '最低发电量'
      }
    ])

    // 监听计算结果变化，更新数字显示
    watch(totalResult, (newVal) => {
      // 更新总发电量
      numberData[0].config.number = [newVal.totalPower]
      numberData[0].config = {...numberData[0].config} // 触发响应式更新

      // 更新平均发电量
      numberData[1].config.number = [parseFloat(newVal.averagePower)]
      numberData[1].config = {...numberData[1].config}

      // 更新最高发电量
      numberData[2].config.number = [newVal.maxPower]
      numberData[2].config = {...numberData[2].config}

      // 更新最低发电量
      numberData[3].config.number = [newVal.minPower]
      numberData[3].config = {...numberData[3].config}
    }, {immediate: true})
    // onMounted(() => {
    //   // setData()
    // })

    // const setData = () => {
    //   dataArr.forEach(e => {
    //     numberData.push({
    //       config: {
    //         number: [e.number],
    //         toFixed: 1,
    //         content: '{nt}',
    //         style: {
    //           fontSize: 24
    //         }
    //       },
    //       text: e.text
    //     })
    //   })
    // }



    return { numberData}
  }
})
</script>

<style lang="scss" scoped>
$box-width: 310px;
$box-height: 400px;

.centreLeft1 {
  padding: 16px;
  height: $box-height;
  width: $box-width;
  border-radius: 10px;

  .bg-color-black {
    height: $box-height - 10px;
    border-radius: 10px;
  }

  .text {
    font-size: 20px;
    color: #c3cbde;
  }

  .dv-dec-3 {
    position: relative;
    width: 100px;
    height: 20px;
    top: -3px;
  }

  .bottom-data {
    .item-box {
      &>div {
        padding-right: 5px;
      }

      font-size: 14px;
      float: right;
      position: relative;
      width: 50%;
      color: #d3d6dd;

      .dv-digital-flop {
        width: 120px;
        height: 30px;
      }

      i {
        font-size: 20px;
        line-height: 30px;
        margin-left: 20px;
      }

      .colorYellow {
        color: yellowgreen;
      }

      p {
        text-align: center;
      }
    }
  }
}
</style>
